<?php

class adminAction extends BaseAction {
    protected $layout = 'admin';
    protected $theme  = '/theme/';
    public function execute() {
        if ($_SESSION['admin'] == '' && $this->params['action'] != 'login') {
            $this->redirect('admin/login');
        }
        $this->view->setLayoutPath(ROOT.$this->theme.'/admin/layout');
        parent::execute();
    }
    
    public function actionLogin() {
        if (!empty($_POST)) {
            $username = $_POST['username'];
            $password = Common::getPassword($_POST['password']);
            $rs       = DAO_Finder::findOne(DB_ADMIN, "user='$username' and pass ='$password'");
            if(!empty($rs)) {
                $_SESSION['admin']  = $rs->getArray();
                $this->redirect('admin/index');
            }
            
        }
        $this->view->layout = '';
        $this->render('login');
    }
    
    public function actionLogout() {
        session_destroy();
        $this->redirect('/');
    }
    
    public function actionIndex() {
        $this->setPageTitle('欢迎');
        $this->render();
    }
    
    public function actionModule() {
        $m = camelize($this->params['m']);
        $a = camelize($this->params['a']);
        $module_class = $m.'Admin';
        
        $module = new $module_class;
        $params = $this->params;
        $params['module'] = $m;
        $params['action'] = $a;
        $module->setParams($params);
        $module->setView($this->view);
        $module->execute();
    }
}

